package com.wenhao.leetcode.medium;

import com.wenhao.leetcode.structure.TreeNode;

import java.util.LinkedList;

/**
 * 题目：https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/
 * 二叉树展开为链表
 * @author Wenhao Tong
 * @create 2021-07-12 16:25
 */
public class LeetCode114 {
    public static void main(String[] args) {

    }

    // 迭代方法
    public void flatten(TreeNode root) {
        LinkedList<TreeNode> stack = new LinkedList<>();
        stack.push(root);
        while (stack.size() > 0) {
            TreeNode temp = stack.pop();
            if (temp.right != null) {
                stack.push(temp.right);
            }
            if (temp.left != null) {
                stack.push(temp.left);
            }
            temp.left = null;
            temp.right = stack.peek();
        }
    }

}
